package com.example.demo.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;

public class ResultBean<T> {
    private static final Logger LOGGER = LoggerFactory.getLogger(ResultBean.class);

    private Integer status;

    /**
     * business code like LMe10001
     * if it is successful response, then the returncode should be 0.
     */
    private String returncode = "0";

    private String message = "";
    
    private Integer pageIndex;
    private Integer pageSize;
    private Long recordsTotal;

    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T payload;

    public ResultBean() {
    }

    /**
     * @param <T>
     * @param payload
     * @return
     */
    public static <T> ResultBean<T> success(T payload) {
        return success(payload, "OK", 0L);
    }
    
    public static ResultBean<String> success(String message) {
        return success(null, message, 0L);
    }
    
    /**
     * Success
     * @param <T>
     * @param payload
     * @param message
     * @param totalCount
     * @return
     */
    public static <T> ResultBean<T> success(T payload, String message, Long totalCount) {
        ResultBean<T> resultBean = new ResultBean<T>();
        resultBean.setStatus(200);
        resultBean.setPayload(payload);
        resultBean.setRecordsTotal(totalCount);
        resultBean.setMessage(message);
        
        // Set the paging information to response body
        if (payload instanceof Page) {
            Page page = (Page)payload;
            PageInfo<T> info = new PageInfo<T>(page);
            resultBean.setRecordsTotal(info.getTotal());
            resultBean.setPageSize(info.getPageSize());
            resultBean.setPageIndex(info.getPageNum());
            LOGGER.debug("Page Info : {}", page.toString());
        }

        return resultBean;
    }

    /**
     * @param <T>
     * @param payload
     * @param message
     * @return
     */
    public static <T> ResultBean<T> success(T payload, String message) {
        return success(payload, message, 0L);
    }

    private static <T> ResultBean<T> error(String returncode, String message, Integer status) {
        ResultBean<T> resultUtil = new ResultBean<>();
        resultUtil.setStatus(status);
        if(returncode != null) {
            resultUtil.setReturncode(returncode);
        }

        resultUtil.setMessage(message);
        
        resultUtil.setPayload(null);
        return resultUtil; 
    }

    public static <T> ResultBean<T> error(String message, Integer status) {
        return error("0", message, status);
    }
    
    /**
     * @param message
     * @return
     */
    public static <T> ResultBean<T> error(String message) {
        return error("0", message, 500);
    }
    

    
    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getReturncode() {
        return returncode;
    }

    public void setReturncode(String returncode) {
        this.returncode = returncode;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getPayload() {
        return payload;
    }

    public void setPayload(T payload) {
        this.payload = payload;
    }

    public Integer getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Long getRecordsTotal() {
        return recordsTotal;
    }

    public void setRecordsTotal(Long recordsTotal) {
        this.recordsTotal = recordsTotal;
    }

}
